<?php
namespace jy_juanfee;
defined('BY_JYA') or exit('error');
/*
	分红插件工具类
*/
class model extends \plugin{
	
	public function create_condition($conditions){
		if(!$conditions){
			return '';
		}
		if(!is_array($conditions)){
			$conditions=json_decode($conditions,true);
		}
		$level_up_types=$this->level_up_types;
		$current_level_up_types=$this->current_level_up_types();
		$str='';
		foreach($current_level_up_types as $key){
			$str.=$level_up_types[$key].'达'.$conditions[$key].';';
		}
		return $str;
	}
	public function current_level_up_types(){
		return str_array($this->setting['level_up_types']);
	}
	public function get_area_code($area){
		if(!$area['province']){
			return '';
		}
		$address=$area['province'];
		if($area['city']){
			$address.=' '.$area['city'];
		}
		if($area['district']){
			$address.=' '.$area['district'];
		}
		if($area['street']){
			$address.=' '.$area['street'];
		}
		$area_code=pdo_getcolumn('core_area',array('address'=>$address),'code');
		if(!$area_code){
			$isup=pdo_count('core_area',array(),'code');
			if($isup==0){
				require ROOT_D.'/update/update_area.php';
			}
		}
		return $area_code;
	}
	public function upgoods($post){
		if(!$post['id'] && !$post['ignore'] && empty($post['commissions'])){
			return false;
		}
		$data=array(
			'goods_id'=>intval($post['goods_id']),
			'ignore'=>intval($post['ignore']),
			'level_id'=>intval($post['level_id']),
			'commissions'=>$post['commissions']?ijson_encode($post['commissions']):'',
		);
		$id=intval($post['id']);
		if($id){
			$this->update('goods',$data,array('id'=>$id));
		}else{
			$data['uniacid']=$_SESSION['uniacid'];
			$this->insert('goods',$data);
		}
		return true;
	}
	public function create($logid=0){
		$isadd=pdo_get('core_credit_log',array('type'=>2,'is_add'=>1,'plugin'=>'jy_juanfee','tag'=>$logid,'uniacid'=>$_SESSION['uniacid']));
		if($isadd['id']){
			exi('该兑换记录已经退过币了！','error');
		}else{
			$log=pdo_get('jy_creditshop_log',array('id'=>$logid,'uniacid'=>$_SESSION['uniacid']));
			$goods=pdo_get('jy_creditshop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$log['goodsid']));
			model('core/member')->member_credit($_SESSION['uid'],'credit2',$goods['price'],true,'jy_juanfee','兑换商品退币到余额');
		}
		$update=array('status'=>3,'time_finish'=>time());
		pdo_update('jy_creditshop_log',$update,array('id'=>$logid,'uniacid'=>$_SESSION['uniacid']));
		return true;
	}
}